package com.stream;

import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamTest {

    public static void main(String[] args) {

        List<Order> orders = Arrays.asList(
                new Order("O001", "U1", 150.0, "Electronics"),
                new Order("O002", "U2", 80.0, "Books"),
                new Order("O003", "U1", 300.0, "Furniture"),
                new Order("O004", "U3", 200.0, "Electronics"),
                new Order("O005", "U2", 450.0, "Clothing")
        );

        List<Order> collect = orders.stream()
                .filter(o -> o.getAmount() > 50)
                .collect(Collectors.toList());
        System.out.println(collect);
        for (Order order : collect) {
//            System.out.println(order.getAmount());
        }

        orders.stream().map(o -> o.getOrderId()).forEach(System.out::println);

        List<Order> collect3 = orders.stream()
                .sorted((o1, o2) -> Double.compare(o2.getAmount(), o1.getAmount()))
                .limit(3)
                .collect(Collectors.toList());

        Map<String, DoubleSummaryStatistics> collect1 = orders.stream()
                .filter(o -> o.getAmount() >= 50)
                .collect(Collectors.groupingBy(Order::getUserId,
                        Collectors.summarizingDouble(Order::getAmount)));
        for (String key : collect1.keySet()) {
            System.out.println(key);
            DoubleSummaryStatistics doubleSummaryStatistics = collect1.get(key);
            System.out.println(doubleSummaryStatistics);
        }

        List<Map.Entry<String, DoubleSummaryStatistics>> collect2 = collect1.entrySet().stream()
                .sorted((e1, e2)
                        -> Double.compare(e1.getValue().getSum(), e2.getValue().getSum()))
                .limit(3).collect(Collectors.toList());

        for (Map.Entry<String, DoubleSummaryStatistics> stringDoubleSummaryStatisticsEntry : collect2) {
            DoubleSummaryStatistics value = stringDoubleSummaryStatisticsEntry.getValue();
            System.out.println(value.getSum());
        }


    }
}
